package mail.verify.chaojiying;

import static com.han.http.util.Print.printlnInfo;

/**
 * Created by ps on 2018/5/16.
 * 图片验证码识别
 */
public class PCodeUtil {

    /**
     * 验证码识别
     *
     * @param codeType 验证码类型
     * @param bytes    图片流
     * @return
     */
    public static String Recognize(String codeType, byte[] bytes, String token) {
        String result = null;
        try {
            result = ChaoJiYing.PostPic(CJYSetting.UserName, CJYSetting.PassWord, CJYSetting.Softid, codeType, "0", bytes);
        } catch (Exception e) {
            result = "打码异常：" + e.getMessage() + ": " + result;
            printlnInfo(result + "\n" + token);
            //OpLogUtils.asyncError(token, result);
        }
        return result;

    }

    /**
     * 提取出验证码
     * @param codeType
     * @param bytes
     * @param token
     * @return
     */
    public static String getVerificationCode(String codeType, byte[] bytes, String token) {
        String rawStr = Recognize(codeType, bytes, token);
        String v = rawStr.substring(rawStr.indexOf("pic_str\":\"") + 10);
        return v.substring(0,v.indexOf("\",\"md5\""));
    }

    /**
     * @param token
     * @param pic_id    图片ID
     * @param bcode     是否需要报错返分
     * @param retryTime 重试次数
     * @return
     */
    public static boolean PCRecognizeErrorFeedback(String token, String pic_id, String pic_str, boolean bcode, int retryTime) {
        boolean needRetry = false;
        String result = null;
        /*try {
            if (bcode){
                OpLogUtils.asyncInfo(token,PromptMessage.picCodeError+":"+retryTime+"识别结果："+pic_str);
                result = ChaoJiYing.ReportError(CJYSetting.UserName,CJYSetting.PassWord, CJYSetting.Softid, pic_id);
                OpLogUtils.asyncInfo(token,"图片识别报错返分:"+result);
                needRetry= retryTime <=CJYSetting.MaxTime;
            }else {
                OpLogUtils.asyncInfo(token, PromptMessage.picCodeRight);
            }
        } catch (Exception e) {
            OpLogUtils.asyncError(token,"图片识别报错返分异常："+e.getMessage()+result);
        }*/

        return needRetry;
    }


}
